//
//  HttpConnection.m
//  TNGS
//
//  Created by songlianying on 14-2-18.
//  Copyright (c) 2014年 hongxin. All rights reserved.
//

#import "HttpConnection.h"
#import "Common.h"
#import "JSONKit.h"
#import "WarningBox.h"

@implementation HttpConnection
{
    NSMutableData *webData;
    NSMutableDictionary *webDic;


}


- (void)sendWebRequest:(NSString *)httpBody :(NSString *)urlWeb
{

    NSURL *url = [NSURL URLWithString: urlWeb];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    if (httpBody != nil) {
        NSString *postString = [NSString stringWithFormat:@"%@",httpBody];
        NSLog(@"发送的字符串是:%@",postString);
        [theRequest setHTTPMethod: @"POST"];
        [theRequest setHTTPBody: [postString dataUsingEncoding: NSUTF8StringEncoding]];
    }

    [NSURLConnection connectionWithRequest:theRequest delegate:self];

    webData = [[NSMutableData alloc] init];
    webDic = [[NSMutableDictionary alloc] init];
}

- (void)sendWebRequest:(NSString *)httpBody :(NSString *)urlWeb :(NSData *)imageData
{
    NSString *urlString = [NSString stringWithFormat:@"%@?%@",urlWeb,httpBody];
    NSMutableString *temp = [[urlString
                              stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                             mutableCopy];

    [temp replaceOccurrencesOfString:@"+"
                          withString:@"%2B"
                             options: NSLiteralSearch
                               range:NSMakeRange(0, [temp length])];

    NSString *outStr = [NSString stringWithString:temp];
    NSURL *url = [NSURL URLWithString: outStr];
    NSLog(@"%@",url);

    NSString *msgLength = [NSString stringWithFormat: @"%d", [imageData length]];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL: url];


    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField: @"Content-Type"];
    [theRequest addValue: msgLength forHTTPHeaderField: @"Content_Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: imageData];

    NSLog(@"%@",theRequest);

    [NSURLConnection connectionWithRequest:theRequest delegate:self];

    webData = [[NSMutableData alloc] init];
    webDic = [[NSMutableDictionary alloc] init];
}


- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{



} // connection:didFailWithrror

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];

} // connection:didReceiveResponse

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData: data];

} // connection:didReceiveData


- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *str = [[NSString alloc] initWithData:webData encoding: NSUTF8StringEncoding];

    //    NSString *decodeString = [TripsDES TripleDES:str encryptOrDecrypt:kCCDecrypt key:gkey];

    NSMutableDictionary *serverResult = [str objectFromJSONString];


    if ([[serverResult objectForKey:@"code"] isEqualToString:@"1000"])
        {
        //退回到登录界面
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[serverResult objectForKey:@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;

        }
    else
        {

        //NSLog(@"收到服务器返回的数据是%@",decodeString);

        [webDic setObject:str forKey:@"soap"];

        [[NSNotificationCenter defaultCenter] postNotificationName:WEBRESULT
                                                            object:self
                                                          userInfo:webDic];
        }

} // connectionDidFinishLoading


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //[WarningBox backTologinPage];
}

@end
